#include "userconfigtable.h"
#include "sqlitemanager.h"
#include <QDebug>
#include <QSqlQuery>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlRecord>

namespace Sqlite{
UserConfigTable::UserConfigTable(QObject *parent) :
    QObject(parent)
{
}


void UserConfigTable::createTable(){

    // create table
    QString str = "CREATE TABLE IF NOT EXISTS userconfig (id INTEGER PRIMARY KEY,";
            str.append("username NVARCHAR(100) , ");
            str.append("pass NVARCHAR(100),");
            str.append("savepass BOOLEAN ),");
            str.append("timeupdate INTEGER )");

    QSqlQuery qry;
    qry.prepare(str );
    SqliteManager::instance()->doProcessSQLQuery(qry);
}

void UserConfigTable::clearTable()
{
    QSqlQuery qry;
    QString strdeleteAll("delete from userconfig");
    qry.prepare(strdeleteAll);
    SqliteManager::instance()->doProcessSQLQuery(qry);
}

void UserConfigTable::insertOrUpdateRecord(const int userId, const QString& userName, const QString& pass){

    QSqlQuery qry;
    QString str = QString("INSERT OR REPLACE INTO userconfig (id, username , pass ) VALUES (%1, '%2', '%3')")
            .arg(userId)
            .arg(userName)
            .arg(pass);

    qry.prepare(str);
    SqliteManager::instance()->doProcessSQLQuery(qry);

}


}

